package com.cf.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cf.common.util.tree.ForestNodeMerger;
import com.cf.system.dto.MenuDTO;
import com.cf.system.entity.Menu;
import com.cf.system.mapper.MenuMapper;
import com.cf.system.service.MenuService;
import com.cf.system.vo.MenuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ChenFeng
 * @date 2021/11/5 10:51 上午
 * @description
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Override
    public List<Map<String, List<String>>> authRoutes() {
        List<Long> roleIds = new ArrayList<>(Arrays.asList(1123598816738675201L));
        List<MenuDTO> routes = baseMapper.authRoutes(roleIds);
        List<Map<String, List<String>>> result = new ArrayList<>();
        return routes.stream().map(e -> {
            Map<String, List<String>> map = new HashMap<>();
            boolean flag = false;
            Map<String, List<String>> oldMap = null;
            for (int i = 0; i < result.size(); i++) {
                flag = result.get(i).containsKey(e.getPath());
                if (flag) {
                    oldMap = result.get(i);
                    break;
                }
            }
            if (flag) {
                List<String> authoritys = oldMap.get(e.getPath());
                authoritys.add(e.getAlias());
                oldMap.put(e.getPath(), authoritys);
                return oldMap;
            }
            map.put(e.getPath(), Arrays.asList(e.getAlias()));
            return map;
        }).collect(Collectors.toList());
    }

    @Override
    public List<MenuVO> buttons() {
        List<Menu> buttons = baseMapper.allButtons();
        List<MenuVO> collect = buttons.stream().map(menu -> {
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menu, menuVO);
            return menuVO;
        }).collect(Collectors.toList());
        return ForestNodeMerger.merge(collect);
    }

    @Override
    public List<MenuVO> routers() {
        List<Menu> allMenus = baseMapper.allMenu();
        List<Menu> roleMenus = allMenus;
        List<Menu> routes = new LinkedList<>(roleMenus);
        roleMenus.forEach(roleMenu -> recursion(allMenus, routes, roleMenu));
        routes.sort(Comparator.comparing(Menu::getSort));
        List<Menu> list = routes.stream().filter(x -> x.getCategory() != null && x.getCategory() == 1).collect(Collectors.toList());
        List<MenuVO> collect = list.stream().map(menu -> {
            MenuVO menuVO = new MenuVO();
            BeanUtils.copyProperties(menu, menuVO);
            return menuVO;
        }).collect(Collectors.toList());
        return ForestNodeMerger.merge(collect);
    }

    private void recursion(List<Menu> allMenus, List<Menu> routes, Menu roleMenu) {
        Optional<Menu> menu = allMenus.stream().filter(x -> x.getId().equals(roleMenu.getParentId())).findFirst();
        if (menu.isPresent() && !routes.contains(menu.get())) {
            routes.add(menu.get());
            recursion(allMenus, routes, menu.get());
        }
    }
}
